<!--
  #%L
  thinkbig-ui-feed-manager
  %%
  Copyright (C) 2017 ThinkBig Analytics
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->
<card-layout body-css="md-padding">
  <header-section>
    <div layout="row" layout-align="space-between-center" flex>
      <div class="card-title" flex>调度</div>

      <div class="card-sub-header">{{vm.stepNumber}} of {{vm.totalSteps}}</div>
    </div>
    <div layout="row" layout-align="space-between-center" flex class="layout-padding-top">
      <div class="card-sub-header" flex>计划并启用管道</div>
      <thinkbig-feed-errors-card-header></thinkbig-feed-errors-card-header>
    </div>
  </header-section>
  <body-section md-content>
    <ng-form name="vm.defineFeedScheduleForm">
      <div layout-gt-sm="row" style="margin-bottom: 18px">

        <md-input-container class="md-block" flex>
          <label>策略</label>
          <md-select ng-model="vm.model.schedule.schedulingStrategy" ng-change="vm.onScheduleStrategyChange()">
            <md-option ng-value="opt.value" ng-repeat="opt in vm.scheduleStrategies">{{ opt.label }}</md-option>
          </md-select>
        </md-input-container>

        <md-input-container class="md-block" flex ng-if="vm.model.schedule.schedulingStrategy == 'CRON_DRIVEN'">


          <label>表达式</label>
          <input required ng-model="vm.model.schedule.schedulingPeriod" name="cronExpression"
                 cron-expression-validator
                 ng-model-options="{ updateOn: 'default blur', debounce: {'default': 300, 'blur': 0} }"/>

          <div ng-show="vm.defineFeedScheduleForm.cronExpression.$touched && vm.defineFeedScheduleForm.cronExpression.$invalid"
               ng-messages="vm.defineFeedScheduleForm.cronExpression.$error">
            <div ng-show="vm.defineFeedScheduleForm.cronExpression.$error.cronExpression"
                 ng-message="cronExpression">无效的cron表达式。
            </div>
            <div ng-show="vm.defineFeedScheduleForm.cronExpression.$error.required" ng-message="required">
              必须.
            </div>
          </div>
          <cron-expression-preview cron-expression="vm.model.schedule.schedulingPeriod"
                                   ng-if="vm.model.schedule.schedulingStrategy == 'CRON_DRIVEN' && vm.defineFeedScheduleForm.cronExpression.$valid"></cron-expression-preview>
        </md-input-container>
        <div flex ng-if="vm.model.schedule.schedulingStrategy == 'TIMER_DRIVEN' || vm.model.schedule.schedulingStrategy == 'PRIMARY_NODE_ONLY'" layout="row" style="margin-bottom:-18px">
          <md-input-container class="md-block" flex>
            <label>运行调度</label>
            <input ng-model="vm.timerAmount" required string-to-number type="number" ng-change="vm.timerChanged()"  ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }"/>
          </md-input-container>

          <md-input-container class="md-block" flex >
            <label>单位</label>
            <md-select ng-model="vm.timerUnits" ng-change="vm.timerChanged()" >
              <md-option value="days">天</md-option>
              <md-option value="hrs">小时</md-option>
              <md-option value="min">分</md-option>
              <md-option value="sec">秒</md-option>
            </md-select>
          </md-input-container>
        </div>

      </div>
      <!--
          <md-input-container class="md-block" >
              <label>Concurrent Tasks</label>
              <input type="number" ng-model="vm.model.schedule.concurrentTasks">
              </input>
          </md-input-container>
          -->
      <div ng-if="vm.isClustered && vm.supportsExecutionNode" style="margin:18px 0">
        <md-input-container class="md-block">
          <label>执行节点</label>
          <md-select ng-model="vm.model.schedule.executionNode" required>
            <md-option value="ALL">所有节点</md-option>
            <md-option value="PRIMARY">主节点</md-option>
          </md-select>
        </md-input-container>
      </div>

      <div ng-if="vm.model.schedule.schedulingStrategy == 'TRIGGER_DRIVEN'">
        <div layout="column">
          <div class="md-subhead">运行前提条件</div>
          <div class="hint">指定执行依赖关系</div>
        </div>
        <div layout="column" layout-align="start start" class="layout-padding-top-bottom">

          <div ng-if="vm.model.schedule.preconditions != null && vm.model.schedule.preconditions.length >0 " layout-fill>
            <md-list>
              <md-list-item ng-repeat="rule in vm.model.schedule.preconditions track by $index" layout-fill class="layout-padding-top-bottom">
                <div layout="column" flex class="md-list-item-text">
                  <span>{{rule.propertyValuesDisplayString}}</span>
                  <span class="hint">{{rule.name}}</span>
                </div>
                            <span class="md-secondary">
                                  <md-button class="md-icon-button" ng-click="vm.showPreconditionDialog($index)">
                                    <ng-md-icon icon="edit"></ng-md-icon>
                                  </md-button>
                            </span>
                <md-divider ng-if="!$last"></md-divider>
              </md-list-item>
            </md-list>
          </div>
          <md-button class="md-primary" ng-click="vm.showPreconditionDialog()">
            添加前提条件
          </md-button>
        </div>

      </div>

      <div layout="column">
        <md-checkbox ng-model="vm.model.active" style="margin-top:18px"> 立即使用管道</md-checkbox>
        <span class="hint fade" ng-show="vm.model.active" ng-hide="!vm.model.active">当它被保存时，提要将被调度并运行。</span>
        <span class="hint fade" ng-show="!vm.model.active" ng-hide="vm.model.active">保存后，管道将被禁用。 您将需要稍后启用管道。</span>
      </div>


      <thinkbig-step-buttons can-continue="vm.isValid && vm.defineFeedScheduleForm.$valid &&  !vm.savingFeed" step-index="{{::vm.stepIndex}}" final-step="创建" on-click-final-button="vm.createFeed()"></thinkbig-step-buttons>

    </ng-form>
  </body-section>
</card-layout>

